package com.bjpowernode.service.impl;

import com.bjpowernode.constant.Constants;
import com.bjpowernode.entity.TClueRemark;
import com.bjpowernode.entity.TCustomerRemark;
import com.bjpowernode.entity.TUser;
import com.bjpowernode.mapper.TCustomerRemarkMapper;
import com.bjpowernode.query.CustomerRemarkQuery;
import com.bjpowernode.service.CustomerRemarkService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerRemarkServiceImpl implements CustomerRemarkService {

    @Resource
    private TCustomerRemarkMapper tCustomerRemarkMapper;

    @Override
    public int saveCustomerRemark(CustomerRemarkQuery customerRemarkQuery) {
        TCustomerRemark tCustomerRemark = new TCustomerRemark();
        BeanUtils.copyProperties(customerRemarkQuery, tCustomerRemark);
        //创建时间
        tCustomerRemark.setCreateTime(new Date());
        //创建人
        TUser authUser = (TUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer currentLoginUserId = authUser.getId();
        tCustomerRemark.setCreateBy(currentLoginUserId);
        return tCustomerRemarkMapper.insertSelective(tCustomerRemark);
    }

    @Override
    public PageInfo<TCustomerRemark> getCustomerRemarkPage(Integer current, Integer customerId) {
        //1.设置PageHelper (第一个参数是查第几页，第二个参数每页查多少条数据)
        PageHelper.startPage(current, Constants.PAGE_SIZE);

        //2.查询数据库表的数据
        List<TCustomerRemark> list = tCustomerRemarkMapper.selectCustomerRemarkPage(customerId);

        //3.封装分页数据到PageInfo
        PageInfo<TCustomerRemark> info = new PageInfo<>(list);

        //4.返回PageInfo对象
        return info;
    }
}
